package net.chinaideal.gridindex.server.util;

import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;

import org.apache.mina.core.buffer.IoBuffer;

public class CodecUtil {

    public static final String  DEFAULT_STRING_ENCODE_UTF_8 = "UTF-8";
    
    public static final Charset CHARSET = Charset.forName(DEFAULT_STRING_ENCODE_UTF_8);

    public static String getStringFromIoBuffer(IoBuffer ioBuffer) throws CharacterCodingException {

        String result = ioBuffer.getString(Charset.forName(DEFAULT_STRING_ENCODE_UTF_8)
            .newDecoder());
        return result;
    }

    public static void putStringToIoBuffer(String string, IoBuffer ioBuffer)
                                                                            throws CharacterCodingException {
        ioBuffer.putString(string, Charset.forName(DEFAULT_STRING_ENCODE_UTF_8).newEncoder());
    }
    
    public static CharsetEncoder getCharsetEncoder() {
        return CHARSET.newEncoder();
    }
    
    public static CharsetDecoder getCharsetDecoder() {
        return CHARSET.newDecoder();
    }

}
